/**
 * listview adapter
 * @author William
 * 2012-2-26
 */
package com.ytu.clouddisk.face;

import java.util.ArrayList;
import java.util.List;

import com.ytu.clouddisk.sdcard.SDCard;

import android.content.Context;
import android.graphics.drawable.LevelListDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class LVAdapter extends BaseAdapter {

	Context mContext;
	ListViewItemData mListViewItemData;
	LayoutInflater mLayoutInflater;
	
	//Toolbar的选项
	

	public LVAdapter(Context context, ListViewItemData listviewItemData) {
		mContext = context;
		mListViewItemData = listviewItemData;
		mLayoutInflater = LayoutInflater.from(mContext);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mListViewItemData.getCount();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mListViewItemData.getItem(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = mLayoutInflater.inflate(R.layout.listview_item, null);
		}
		ImageView mImageViewLeft = (ImageView) convertView
				.findViewById(R.id.imageviewleft);

		TextView mTextView = (TextView) convertView.findViewById(R.id.textview);
		ImageView mImageViewRight = (ImageView) convertView
				.findViewById(R.id.imageviewright);

		String fileName = mListViewItemData.getName(position);
       //判断文件后缀名，使用相应图片
		if (checkEndsWithInStringArray(fileName, mContext.getResources()
				.getStringArray(R.array.fileEndingImage))) {
			mImageViewLeft.setImageResource(R.drawable.listview_image);
		} else if (checkEndsWithInStringArray(fileName, mContext.getResources()
				.getStringArray(R.array.fileEndingAudio))) {
			mImageViewLeft.setImageResource(R.drawable.listview_music);
		} else if (checkEndsWithInStringArray(fileName, mContext.getResources()
				.getStringArray(R.array.fileEndingVideo))) {
			mImageViewLeft.setImageResource(R.drawable.listview_video);
		} else if (checkEndsWithInStringArray(fileName, mContext.getResources()
				.getStringArray(R.array.fileEndingPackage))) {
			mImageViewLeft.setImageResource(R.drawable.listview_archive);
		} else if (checkEndsWithInStringArray(fileName, mContext.getResources()
				.getStringArray(R.array.fileEndingText))) {
			mImageViewLeft.setImageResource(R.drawable.listview_archive);
		}else{
			mImageViewLeft.setImageResource(R.drawable.listview_folder);
		}
		mTextView.setText(mListViewItemData.getTitle(position));

		mImageViewRight.setImageResource(R.drawable.list_item_arrow);
		mImageViewLeft.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				itemClick(position);
			}
		});
		mTextView.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				itemClick(position);
			}
		});
		return convertView;
	}

	protected void itemClick(int position) {
		//判断填充的数据源
		{
			
		}
	    SDCard sdCard = SDCard.getInstance();
		if (mListViewItemData.getTitle(position).equals("根目录")) {
			sdCard.browseRoot();
		} else if (mListViewItemData.getTitle(position).equals("上一级")) {
			sdCard.upLevel();
		} else {
			sdCard.browseFile(mListViewItemData.getTitle(position));
		}
		Toast.makeText(mContext, sdCard.getCurrentDir(), Toast.LENGTH_SHORT)
				.show();
		List<String> file_name_array = new ArrayList<String>();
		file_name_array = sdCard.getFilesName();
		// 非根目录添加返回上一级和根目录项
		if (!sdCard.getCurrentDir().equals("/sdcard")) {
			file_name_array.add("上一级");
			file_name_array.add("根目录");
		}
		LevelListDrawable levelListDrawable = (LevelListDrawable) mContext
				.getResources().getDrawable(R.drawable.listview_image_file); // 获取图片列表
		ListViewItemData mListViewItemData = new ListViewItemData(
				levelListDrawable, file_name_array, file_name_array.size());
		FileExplorer.getListView().setAdapter(
				new LVAdapter(mContext, mListViewItemData));

	}

	/* 判断文件类型 */
	public boolean checkEndsWithInStringArray(String checkItsEnd,
			String[] fileEndings) {
		for (String aEnd : fileEndings) {
			if (checkItsEnd.endsWith(aEnd))
				return true;
		}
		return false;
	}
}
